Imports System.Data.SqlClient
Imports System.Collections.Generic

Partial Public Class VendorPage
    Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.User.IsInRole("Administrator") And SiteSettings.OnlyAdminsCanEdit Then
            btnDeleteVendor.Enabled = False
            btnDeleteVendorAndItems.Enabled = False
            btnSave.Enabled = False
        Else
            btnDeleteVendor.Enabled = True
            btnDeleteVendorAndItems.Enabled = True
            btnSave.Enabled = True
        End If
        If Not IsPostBack Then
            If Not Request.QueryString("id") Is Nothing Then
                If Request.QueryString("id") = "new" Then
                    ' NEW
                    BuildNew()
                Else
                    ' item
                    GetVendor(New Guid(Request.QueryString("id")))
                End If
            ElseIf Not Page.RouteData.Values("vendor") Is Nothing Then
                GetVendor(New Guid(Page.RouteData.Values("vendor").ToString))
            Else
                ' NEW
                BuildNew()
            End If
        End If
    End Sub
    Private Sub BuildNew()
        Me.Title = "ITracker: Add Vendor"
        Me.lblPageHeader.Text = "New Vendor Details: "
        lblID.Text = ""
        txtVendorAddress1.Text = ""
        txtVendorAddress2.Text = ""
        txtVendorCity.Text = ""
        txtVendorName.Text = ""
        txtVendorState.Text = ""
        txtVendorZip.Text = ""
        btnDeleteVendor.Enabled = False
        btnDeleteVendorAndItems.Enabled = False
    End Sub
    Private Sub GetVendor(ByVal id As Guid)
        Me.Title = "ITracker: Vendor Details"
        Dim _vendor As Vendor = Vendor.Fetch(id)
        lblPageHeader.Text = "Vendor Details: " & _vendor.Name
        lblID.Text = id.ToString
        txtVendorName.Text = _vendor.Name
        txtVendorAddress1.Text = IIf(_vendor.Address1 Is DBNull.Value, "", _vendor.Address1)
        txtVendorAddress2.Text = IIf(_vendor.Address2 Is DBNull.Value, "", _vendor.Address2)
        txtVendorCity.Text = IIf(_vendor.City Is DBNull.Value, "", _vendor.City)
        txtVendorState.Text = IIf(_vendor.State Is DBNull.Value, "", _vendor.State)
        txtVendorZip.Text = IIf(_vendor.Zip Is DBNull.Value, "", _vendor.Zip)
    End Sub

    Protected Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        lblError.Text = ""
        Dim _vendor As New Vendor
        If lblID.Text.Trim.Length > 0 Then
            _vendor = Vendor.Fetch(New Guid(lblID.Text.Trim))
        End If
        With _vendor
            .Name = txtVendorName.Text.Trim
            .Address1 = txtVendorAddress1.Text.Trim
            .Address2 = txtVendorAddress2.Text.Trim
            .City = txtVendorCity.Text.Trim
            .State = txtVendorState.Text.Trim
            .Zip = txtVendorZip.Text.Trim
        End With
        
            If Vendor.Save(_vendor) Then
                Response.Redirect(Utils.RelativeWebRoot & "Default.aspx")
            Else
                lblError.Text = "Error saving vendor."
            End If


    End Sub

    Protected Sub btnDeleteVendor_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteVendor.Click
        Try
            Dim msgOut As String = String.Empty
            If Vendor.Delete(New Guid(lblID.Text.Trim), False, msgOut) Then
                Response.Redirect("~/")
            Else
                Throw New Exception(msgOut)
            End If
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            lblError.Text = ex.Message
        End Try
    End Sub

    Protected Sub btnDeleteVendorAndItems_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteVendorAndItems.Click
        Try
            Dim msgOut As String = String.Empty
            If Vendor.Delete(New Guid(lblID.Text.Trim), True, msgOut) Then
                Response.Redirect("~/")
            Else
                Throw New Exception(msgOut)
            End If
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            lblError.Text = ex.Message
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If Not Request.QueryString("id") Is Nothing Then
            If Request.QueryString("id") = "new" Then
                ' NEW
                BuildNew()
            Else
                ' item
                GetVendor(New Guid(Request.QueryString("id")))
            End If
        ElseIf Not Page.RouteData.Values("vendor") Is Nothing Then
            GetVendor(New Guid(Page.RouteData.Values("vendor").ToString))
        Else
            ' NEW
            BuildNew()
        End If
    End Sub
End Class